<?php

namespace Frontend\Controllers;

use Models\Doctypes;
use Models\References;
use Forms\SearchForm;

class IndexController extends ControllerBase {

    public function indexAction() {
        $limit = References::findFirstById('limit_in_day');
        if ($limit) {
            $limit_in_day = $limit->value;
        } else {
            $limit_in_day = 1;
        }
        $query = $this->modelsManager->createBuilder()
                ->columns(array(
                    'Models\Documents.id',
                    'Models\Documents.title',
                    'Models\Documents.description',
                    'Models\Documents.submit_date',
                    'Models\Documents.cost',
                    'Models\Documents.link',
                    'Models\Documents.image',
                    'Models\Documents.topic_id',
                    'Models\Documents.download',
                    'Models\Documents.discount',
                    'Topics.name TopicName',
                ))
                ->from('Models\Documents')
                ->leftjoin('Models\Topics', 'Topics.id = Models\Documents.topic_id', 'Topics')
                ->leftjoin('Models\Downloads', 'Downloads.doc_id = Models\Documents.id', 'Downloads')
                ->groupby('Models\Documents.id')
                /* ->orderby('count(Downloads.doc_id)') */
                ->orderby('Models\Documents.download DESC')
                ->where('Models\Documents.public = 1')
                ->limit($limit_in_day);
        $docs_in_day = $query->getQuery()->execute();
        $this->view->docs_in_day = $docs_in_day;

        $doctypes = Doctypes::find()->toArray();
        foreach ($doctypes as $key => $type) {
            $docs = $query->where("Topics.doctype_id = '" . $type['id'] . "'")
                            ->andwhere('Models\Documents.public = 1')
                            ->getQuery()->execute();
            $doctypes[$key]['docs_in_day'] = $docs;
        }
        $this->view->doctypes = $doctypes;
    }

    public function searchAction() {
        $text_search = $this->request->getPost('text_search');
        $currentPage = (int) $this->request->getPost('page');
        if (!$currentPage) {
            $currentPage = 1;
        }
        $limit = (int) $this->request->getPost('limit');
        if (!$limit) {
            $limit = 10;
        }
        if ($this->request->getPost('text_search')) {
            $query = $this->modelsManager->createBuilder()
                    ->columns(array(
                        'Models\Documents.id',
                        'Models\Documents.title',
                        'Models\Documents.description',
                        'Models\Documents.submit_date',
                        'Models\Documents.cost',
                        'Models\Documents.link',
                        'Models\Documents.image',
                        'Models\Documents.topic_id',
                        'Models\Documents.download',
                        'Models\Documents.discount',
                        'Topics.name TopicName',
                        'Doctypes.name DoctypeName',
                    ))
                    ->from('Models\Documents')
                    ->leftjoin('Models\Topics', 'Topics.id = Models\Documents.topic_id', 'Topics')
                    ->leftjoin('Models\Doctypes', 'Doctypes.id = Topics.doctype_id', 'Doctypes')
                    ->where("title  LIKE '%$text_search%' OR description  LIKE '%$text_search%'")
                    ->andwhere('Models\Documents.public = 1');
            $docs = $query->getQuery()->execute();
            $pagintion = new \Phalcon\Paginator\Adapter\Model(
                    array(
                "data" => $docs,
                "limit" => $limit,
                "page" => $currentPage
                    )
            );
            $this->view->docs = $pagintion->getPaginate();
            $types = Doctypes::find();
            $this->view->types = $types;
        } else {
            $this->dispatcher->forward(array('action' => 'index'));
        }
        $this->view->text_search = $text_search;
        $this->view->page = $currentPage;
        $this->view->limit = $limit;
    }

}
